//
//  OrderDetailsViewController.m
//  BusinessApp
//
//  Created by CccDaxIN on 2016/10/14.
//  Copyright © 2016年 macox. All rights reserved.
//

#import "OrderDetailsViewController.h"
#import "EssentialInformation.h"
#import "OrderDetailsModel.h"
#import "OrderTableViewCell.h"
@interface OrderDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    EssentialInformation *essentialInformationV;
    NSDictionary *products_dict;
    UIView *homepageFooterView;
}
@property (nonatomic,strong)NSMutableArray *data_array;
@property (nonatomic,strong)NSMutableArray *arrayImage;
@property (nonatomic,strong)UITableView *orderTableView;

@end

@implementation OrderDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createOrderDetailsTableView];
    self.title = @"订单详情";
    [NNSingleton sharedSingleton].ShiJiDaoZhangJinE = @"1";
    _data_array = [[NSMutableArray alloc]init];
    _arrayImage = [[NSMutableArray alloc]init];
    products_dict = [[NSDictionary alloc]init];
    [SVProgressHUD showWithStatus:@"正在加载"];

    [self displayDetailDataRequest];
    
    /*
     *返回
     */
    UIImage *OrderDetailsBackImage = [UIImage imageNamed:@"左箭头"];
    OrderDetailsBackImage = [OrderDetailsBackImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:OrderDetailsBackImage style:UIBarButtonItemStylePlain target:self action:@selector(OrderDetailsBackAction)];
    
    /*
     收缩状态
     */
    [_shrinkButton setImage:[UIImage imageNamed:@"zuojiantou@3x-拷贝-8"] forState:UIControlStateSelected];
    _shrinkButton.selected = NO;
    
    // Do any additional setup after loading the view from its nib.
}
#pragma mark -- 展示数据
- (void)displayDetailDataRequest{
    [GDJHttpTool get:Orderdetails params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"OrderId":_ProID} success:^(id responseObj) {
        NSLog(@"喜欢你 %@",responseObj[@"Order"]);
        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"哈哈哈 %ld",(long) err);
        if (err == 0) {
            [SVProgressHUD dismiss];
            _data_array =   [OrderDetailsModel mj_objectArrayWithKeyValuesArray:responseObj[@"Order"]];
            [NNSingleton sharedSingleton].commodityID = _ProID;

            for (NSArray *array in responseObj[@"Order"]) {
                [_arrayImage addObject:array];
            }
            for (NSDictionary *dictv in _arrayImage) {
                _arrayImage =   [OrderDetailsModel mj_objectArrayWithKeyValuesArray:[dictv objectForKey:@"Products"]];
            }
            [self.orderTableView reloadData];
            [self displayDataSubView];
        }
    }failure:^(NSError *error) {
    }];
}

#pragma mark --  创建tableview
- (void)createOrderDetailsTableView{
    self.orderTableView =[[UITableView alloc]initWithFrame:CGRectMake(0,95, kScrernWidth,kScrernHeight-95) style:UITableViewStyleGrouped];
    self.orderTableView.showsVerticalScrollIndicator = FALSE;
    self.orderTableView.delegate=self;
    self.orderTableView.dataSource=self;
    self.orderTableView.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.94 alpha:1.00];
    self.orderTableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.orderTableView];
    
    essentialInformationV = [[EssentialInformation alloc]initWithFrame:CGRectMake(0,0, kScrernWidth,340)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrayImage.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)sectio{
    NSInteger returnHeight = 305;
    if (_data_array.count != 0) {
        OrderDetailsModel *model = [_data_array objectAtIndex:0];
        NSString *string_statues = [NSString stringWithFormat:@"%d",model.OrderStatues];
        if ([string_statues isEqualToString:@"5"]) {
            returnHeight = 340;
        }
    }
    return returnHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderTableViewCell  *orderCell= [tableView dequeueReusableCellWithIdentifier:@"orderTab"];
    if (orderCell == nil) {
        orderCell = [[[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:nil options:nil] firstObject];
    }
    orderCell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_arrayImage.count != 0) {
        OrderDetailsModel *model = [_arrayImage objectAtIndex:indexPath.row];
        NSLog(@"法医秦明 ；；；%@",model.ProductName);
        orderCell.model = model;
    }
    return orderCell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    homepageFooterView = [[UIView alloc]init];
    if (_data_array.count != 0) {
        OrderDetailsModel *model = [_data_array objectAtIndex:0];
        NSString *string_statues = [NSString stringWithFormat:@"%d",model.OrderStatues];
        if ([string_statues isEqualToString:@"5"]) {
            homepageFooterView.frame =CGRectMake(0, 0, kScrernWidth, 340);
        }
    }else{
        homepageFooterView.frame =CGRectMake(0, 0, kScrernWidth, 294);
    }

    homepageFooterView.backgroundColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1];
    [self.view addSubview:homepageFooterView];
    
    
    [homepageFooterView addSubview:essentialInformationV];

    return homepageFooterView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

- (void)displayDataSubView{
    
    OrderDetailsModel *model = [_data_array objectAtIndex:0];
    //总价
    NSString *OrderTotalAmount_str = [NSString stringWithFormat:@"%.2f元",model.OrderTotalAmount];
    essentialInformationV.totalLabel.text = OrderTotalAmount_str;
    
    //优惠卷抵扣
//    NSString *CouponRecord_str = [NSString stringWithFormat:@"%.2f元",model.CouponRecord];
//    essentialInformationV.redLabel.text = CouponRecord_str;
    
    //实际到账金额
    NSString *ShiJiDaoZhangJinE_str = [NSString stringWithFormat:@"%.2f元",model.ShiJiDaoZhangJinE];
    essentialInformationV.actualPayAmount.text = ShiJiDaoZhangJinE_str;
    
    /*
     订单号
     */
    NSString *string = [NSString stringWithFormat:@"%@",model.OrderCode];
    essentialInformationV.orderNumberLabel.text = string;
    /*
     订单状态
     */
    NSString *string_statues = [NSString stringWithFormat:@"%d",model.OrderStatues];
    if ([string_statues isEqualToString:@"1"]) {
        essentialInformationV.orderStatusLabel.text = @"待付款";
    }else if ([string_statues isEqualToString:@"4"]){
        essentialInformationV.orderStatusLabel.text = @"已关闭";
    }else if ([string_statues isEqualToString:@"5"]){
        
        if ([[NNSingleton sharedSingleton].ShiJiDaoZhangJinE isEqualToString:@"1"]) {
            _ShiJiDaoZhangJinEView = [[UIView alloc]initWithFrame:CGRectMake(0, 294, kScrernWidth, 40)];
            _ShiJiDaoZhangJinEView.backgroundColor = [UIColor whiteColor];
            [essentialInformationV addSubview:_ShiJiDaoZhangJinEView];
            
            UILabel *labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 100, 20)];
            labelTitle.text = @"实际到账金额：";
            labelTitle.font = [UIFont systemFontOfSize:13];
            [_ShiJiDaoZhangJinEView addSubview:labelTitle];
            /*
             *实际到帐金额
             */
            UILabel *dataLabel = [[UILabel alloc]initWithFrame:CGRectMake(105, 10, kScrernWidth-100, 20)];
            NSString *stringTotalAmount = [NSString stringWithFormat:@"%.2f元",model.ShiJiDaoZhangJinE];
            dataLabel.font = [UIFont systemFontOfSize:13];
            dataLabel.text = stringTotalAmount;
            [_ShiJiDaoZhangJinEView addSubview:dataLabel];
          
        }else{
            [_ShiJiDaoZhangJinEView removeFromSuperview];
        }
       
        essentialInformationV.orderStatusLabel.text = @"已完成";
    }else if ([string_statues isEqualToString:@"7"]){
        essentialInformationV.orderStatusLabel.text = @"未评价";
    }else if ([string_statues isEqualToString:@"2"]){
        essentialInformationV.orderStatusLabel.text = @"待发货";
    }else if ([string_statues isEqualToString:@"3"]){
        essentialInformationV.orderStatusLabel.text = @"待收货";
    }
    /*
     卖家昵称
     */
    essentialInformationV.sellerNicknameLabel.text = model.UserName;
    /*
     下单时间
     */
    essentialInformationV.orderTimeLabel.text = model.OrderDate;
    /*
     支付时间
     */
    essentialInformationV.paymentTimeLabel.text = model.PayDate;
    //收货人
    essentialInformationV.consigneeLabel.text = model.ShipTo;
    //手机号
    essentialInformationV.iphoneNumber.text = model.Phone;
    //地址
    essentialInformationV.receiptAddressLabel.text = model.Address;
    //发货时间
    essentialInformationV.receivingTime.text = model.ShippingDate;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
#pragma mark --返回
- (void)OrderDetailsBackAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- 展示收缩商品详情
- (IBAction)shrinkCommodity:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        NSLog(@"=======  关");
        [_shrinkButton setImage:[UIImage imageNamed:@"youjiantou"] forState:UIControlStateSelected];
        self.orderTableView.hidden = YES;
        essentialInformationV.frame = CGRectMake(0, 95, kScrernWidth, 340);
        [self.view addSubview:essentialInformationV];
    }else{
        NSLog(@"=======  开");
        [_shrinkButton setImage:[UIImage imageNamed:@"zuojiantou@3x-拷贝-8"] forState:UIControlStateSelected];
        self.orderTableView.hidden = NO;
        essentialInformationV.frame = CGRectMake(0, 0, kScrernWidth, 340);
        [homepageFooterView addSubview:essentialInformationV];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
